package com.ghy.controller;

import com.ghy.constant.ML;
import com.ghy.dto.OrderDetailInsertDTO;
import com.ghy.dto.OrderDetailPageDTO;
import com.ghy.dto.OrderDetailUpdateDTO;
import com.ghy.entity.OrderDetail;
import com.ghy.excel.OrderDetailExcelDTO;
import com.ghy.service.OrderDetailService;
import com.ghy.util.EasyExcelUtil;
import com.ghy.vo.OrderDetailSimpleListVO;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单明细明细表 控制层。
 *
 * @author ghy
 * @since 2024-12-22
 */
@Tag(name = "订单明细")
@RestController
@RequestMapping("/orderDetail")
public class OrderDetailController {

    @Autowired
    private OrderDetailService orderDetailService;

    @Operation(summary = "新增-单条", description = "新增一条订单明细记录")
    @PostMapping("insert")
    public boolean insert(@Validated @RequestBody OrderDetailInsertDTO dto) {
        return orderDetailService.insert(dto);
    }

    @Operation(summary = "查询-单条", description = "按主键查询一条订单明细记录")
    @GetMapping("select/{id}")
    public OrderDetail select(@PathVariable("id") Long id) {
        return orderDetailService.select(id);
    }

    @Operation(summary = "查询-简单列表", description = "查询全部订单明细记录,仅返回简单信息")
    @GetMapping("simpleList")
    public List<OrderDetailSimpleListVO> simpleList() {
        return orderDetailService.simpleList();
    }

    @Operation(summary = "查询-分页", description = "分页查询订单明细记录")
    @GetMapping("page")
    public Page<OrderDetail> page(@Validated OrderDetailPageDTO dto) {
        return orderDetailService.page(dto);
    }

    @Operation(summary = "修改-单条", description = "按主键修改一条订单明细记录")
    @PutMapping("update")
    public boolean update(@RequestBody OrderDetailUpdateDTO dto) {
        return orderDetailService.update(dto);
    }

    @Operation(summary = "删除-单条", description = "按主键删除一条订单明细记录")
    @DeleteMapping("delete/{id}")
    public boolean delete(@PathVariable("id") Long id) {
        return orderDetailService.delete(id);
    }

    @Operation(summary = "删除-批量", description = "按主键批量删除订单明细记录")
    @DeleteMapping("deleteBatch")
    public boolean deleteBatch(@RequestParam("ids") List<Long> ids) {
        return orderDetailService.deleteBatch(ids);
    }

    @Operation(summary="查询——报表打印",description ="打印订单明细相关的报表数据")
    @GetMapping("excel")
    public void excel(HttpServletResponse response) {
        EasyExcelUtil.download(response,
                ML.Order.EXCEL_FILE_NAME,
                ML.Order.EXCEL_SHEET_NAME,
                OrderDetailExcelDTO.class,
                orderDetailService.excel());
    }

}
